using System;

using Microsoft.SharePoint.WebControls;

using Synergy.Extensions;

namespace Synergy.SharePoint.WebControls.Extensions
{
	/// <summary>
	/// Extension methods for the <see cref="SPCalendarView"/> class.
	/// </summary>
	/// <remarks>
	///		<list type="table">
	///			<listheader><term>Version</term><description>Description</description></listheader>
	///			<item>
	///				<term>1.0</term>
	///				<description>Initial version</description>
	///			</item>
	///		</list>
	/// </remarks>
	[CLSCompliant(false)]
	public static class SPCalendarViewExtension
	{
		/// <summary>
		/// Gets the selected date of the specified <paramref name="view"/>.
		/// </summary>
		/// <param name="view">The view.</param>
		/// <returns>The selected date.</returns>
		/// <exception cref="ArgumentNullException"><paramref name="view"/> is <c>null</c>.</exception>
		public static DateTime GetSelectedDate(this SPCalendarView view)
		{
			// validate params
			if (view == null)
			{
				throw new ArgumentNullException("view");
			}

			// parse date with today as the default
			var date = DateTime.Today;
			if (!String.IsNullOrEmpty(view.SelectedDate))
			{
				DateTime.TryParse(view.SelectedDate, out date);
			}
			return date;
		}


		/// <summary>
		/// Gets the first visible date within the specified <paramref name="view"/>.
		/// </summary>
		/// <param name="view">The calendar view.</param>
		/// <returns>The first date visible in the <paramref name="view"/>.</returns>
		/// <exception cref="ArgumentNullException"><paramref name="view"/> is <c>null</c>.</exception>
		public static DateTime GetFirstVisibleDate(this SPCalendarView view)
		{
			// validate params
			if (view == null)
			{
				throw new ArgumentNullException("view");
			}

			// determine date based on view type
			var date = view.GetSelectedDate().GetMidnight();
			switch (view.ViewType.ToLowerInvariant())
			{
				case "month":
					date = date.GetFirstDayOfMonth().GetFirstDayOfWeek();
					break;

				case "week":
					date = date.GetFirstDayOfWeek();
					break;

				case "day":
					// do nothing
					break;
			}
			return date;
		}


		/// <summary>
		/// Gets the last visible date within the specified <paramref name="view"/>.
		/// </summary>
		/// <param name="view">The calendar view.</param>
		/// <returns>The last date visible in the <paramref name="view"/>.</returns>
		/// <exception cref="ArgumentNullException"><paramref name="view"/> is <c>null</c>.</exception>
		public static DateTime GetLastVisibleDate(this SPCalendarView view)
		{
			// validate params
			if (view == null)
			{
				throw new ArgumentNullException("view");
			}

			// determine date based on view type
			var date = view.GetSelectedDate().GetMidnight();
			switch (view.ViewType.ToLowerInvariant())
			{
				case "month":
					date = date.GetLastDayOfMonth().GetLastDayOfWeek();
					break;
				
				case "week":
					date = date.GetLastDayOfWeek();
					break;

				case "day":
					date = date.GetEndOfDay();
					break;
			}
			return date;
		}
	}
}